package me.firstapp.repository.bbs.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.MemberPointAccountTrace;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.bbs.MemberPointAccountTraceRepository;

@Repository
public class MemberPointAccountTraceRepositoryImpl extends EntityRepositoryImpl<MemberPointAccountTrace>
		implements MemberPointAccountTraceRepository {

	@Override
	protected Class<MemberPointAccountTrace> getPersistentClass() {
		return MemberPointAccountTrace.class;
	}

	@Override
	public Page<MemberPointAccountTrace> findPointAccountTracePage(Integer pageNo, Integer pageSize,
			Long pointAccountId, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM MemberPointAccountTrace bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" AND bean.pointAccountId=:pointAccountId ");
		parameters.put("pointAccountId", pointAccountId);

		return super.queryForPage(MemberPointAccountTrace.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public Page<MemberPointAccountTrace> findPointAccountTracePageByParams(Long pointAccountId, Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Date startTime, Date endTime, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM MemberPointAccountTrace bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" AND bean.pointAccountId=:pointAccountId ");
		parameters.put("pointAccountId", pointAccountId);
		
		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					sql.append(" AND bean." + key + " LIKE :value ");
					parameters.put("value", "%" + value + "%");
				}
			}
		}
		Date _startTime = null;
		if (startTime != null) {
			String startTimeStr = DateUtils.getDateStr(startTime, "yyyy-MM-dd") + " 00:00:00";
			_startTime = DateUtils.getDateFromDateStr(startTimeStr, "yyyy-MM-dd HH:mm:ss");

		}
		Date _endTime = null;
		if (endTime != null) {
			String endTimeStr = DateUtils.getDateStr(endTime, "yyyy-MM-dd") + " 23:59:59";
			_endTime = DateUtils.getDateFromDateStr(endTimeStr, "yyyy-MM-dd HH:mm:ss");
		}

		if (_startTime != null) {
			sql.append(" AND bean.changeTime >=:startTime ");
			parameters.put("startTime", _startTime);
		}
		if (_endTime != null) {
			sql.append(" AND bean.changeTime <=:endTime");
			parameters.put("endTime", _endTime);
		}
		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(MemberPointAccountTrace.class, sql.toString(), parameters, pageNo, pageSize);
	}

}
